package com.ysd.zytj.service;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.ysd.zytj.dao.IConsumeLogDao;
import com.ysd.zytj.dao.IMemberDao;
import com.ysd.zytj.model.ConsumeLog;
import com.ysd.zytj.model.Member;

@Service
@Transactional
public class ConsumeLogService implements IConsumeLogService {

	@Resource
	private IConsumeLogDao consumeLogDao;
	@Resource
	private IMemberDao mumberDao;
	
	@Override
	public String save(ConsumeLog c) {
		Member m = mumberDao.findByCardNo(c.getCardNo());
		if(m.getStatus()>0) {
			if(c.getReadRoomId()>0){
				return "error";
			}
			ConsumeLog con = consumeLogDao.getOne(c.getCardNo());
			con.setOutTime(new Date());//出来时间设为刷卡时间
			consumeLogDao.save(con);//修改离开时间
			m.setStatus(0);
		}else {//进去
			Calendar cal = Calendar.getInstance();
			cal.setTime(new Date());
			cal.add(Calendar.MINUTE, 30);//默认离开时间是进入时间+30分
			c.setOutTime(cal.getTime());
			consumeLogDao.save(c);//添加进入日志
			m.setStatus(c.getReadRoomId());//修改会员卡状态
		}
		mumberDao.save(m);
		return "success";
	}
	
	@Override
	public String in(ConsumeLog c) {
		Member m = mumberDao.findByCardNo(c.getCardNo());
		if(m.getStatus()>0) {
			return "error";
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.MINUTE, 30);//默认离开时间是进入时间+30分
		c.setOutTime(cal.getTime());
		consumeLogDao.save(c);//添加进入日志
		m.setStatus(c.getReadRoomId());//修改会员卡状态
		mumberDao.save(m);
		return "success";
	}
	
	@Override
	public String out(ConsumeLog c) {
		Member m = mumberDao.findByCardNo(c.getCardNo());
		ConsumeLog con = consumeLogDao.getOne(c.getCardNo());
		con.setOutTime(new Date());//出来时间设为刷卡时间
		consumeLogDao.save(con);//修改离开时间
		m.setStatus(0);
		mumberDao.save(m);
		return "success";
	}

}
